package com.szcinda.controller;

import com.szcinda.controller.configuration.UserLoginToken;
import com.szcinda.repository.Organization;
import com.szcinda.repository.User;
import com.szcinda.service.dto.organization.OrganizationCreateDto;
import com.szcinda.service.dto.organization.OrganizationService;
import com.szcinda.service.dto.organization.OrganizationTree;
import com.szcinda.service.dto.organization.OrganizationUpdateDto;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping({"organization"})
public class OrganizationController {

    private final OrganizationService organizationService;

    public OrganizationController(OrganizationService organizationService) {
        this.organizationService = organizationService;
    }

    @UserLoginToken
    @PostMapping("create")
    @OperationLogAnnotation(operDesc = "新增组织", operModel = "组织机构")
    public Result<String> create(@RequestBody OrganizationCreateDto createDto) {
        organizationService.create(createDto);
        return Result.success();
    }

    @UserLoginToken
    @PostMapping("update")
    @OperationLogAnnotation(operDesc = "修改组织", operModel = "组织机构")
    public Result<String> update(@RequestBody OrganizationUpdateDto updateDto) {
        organizationService.update(updateDto);
        return Result.success();
    }

    @GetMapping("using/{id}")
    @OperationLogAnnotation(operDesc = "激活组织", operModel = "组织机构")
    public Result<String> using(@PathVariable String id) {
        organizationService.use(id);
        return Result.success();
    }

    @GetMapping("unused/{id}")
    @OperationLogAnnotation(operDesc = "禁用组织", operModel = "组织机构")
    public Result<String> unused(@PathVariable String id) {
        organizationService.unused(id);
        return Result.success();
    }

    @GetMapping("getById/{id}")
    public Result<Organization> getById(@PathVariable String id) {
        return Result.success(organizationService.getById(id));
    }

    @UserLoginToken
    @GetMapping("tree/{userId}")
    public List<OrganizationTree> tree(@PathVariable String userId) {
        return organizationService.findCurrentTree(userId);
    }

    @GetMapping("getUsers/{userId}")
    public Result<List<User>> getUsers(@PathVariable String userId) {
        return Result.success(organizationService.getUsers(userId));
    }

    @UserLoginToken
    @GetMapping("getTree")
    public List<OrganizationTree> getTree() {
        return organizationService.findTree();
    }

    @UserLoginToken
    @GetMapping("getTree2/{userId}")
    public List<OrganizationTree> getTree2(@PathVariable String userId) {
        return organizationService.findCurrentTree(userId);
    }

    @UserLoginToken
    @GetMapping("delete/{orgId}/{userId}")
    @OperationLogAnnotation(operDesc = "删除组织", operModel = "组织机构")
    public Result<String> delete(@PathVariable String orgId, @PathVariable String userId) {
        organizationService.delete(orgId, userId);
        return Result.success();
    }
}
